package org.paybiz.core;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.paybiz.core.service.callback.PayCallbackService;
import org.paybiz.model.PayResultDO;
import org.springframework.util.Assert;

/**
 * 支付回调处理类,通过applicationContext_pay.xml配置文件实例
 * @author nassir.wen
 * @date 2013-1-7下午3:58:25
 * @company: MSD. 
 * @copyright: Copyright (c) 2013
 */
public class PayCallbackHandler {

    private static final Log logger = LogFactory.getLog(PayCallbackHandler.class);

    private Map<String, PayCallbackService> payCallbackServiceMap;

    /**
     * 支付对应第三方账户参数名
     */
    private Map<String, String> payMerIdParamMap;

    /**
     * 效验回调参数结果
     * @author nassir.wen
     * @date 2013-1-7下午3:55:29
     * @return
     */
    public PayResultDO verifyPayCallbackResult(HttpServletRequest request, HttpServletResponse response) {
        return getTargetService(request).payCallback(request, response);
    }

    /**
     * 发送异步通知结果到第三方
     * @author nassir.wen
     * @date 2013-1-10上午10:39:41
     * @param request
     * @param response
     * @param result
     */
    public void sendAsynResult(HttpServletRequest request, HttpServletResponse response, int result) {
        getTargetService(request).sendAsynResult(response, result);
    }

    /**
     * 获取到执行service
     * @author nassir.wen
     * @date 2013-1-8上午10:55:41
     * @param request
     * @return
     */
    private PayCallbackService getTargetService(HttpServletRequest request) {
        String callbackType = judgeCallbackType(request);
        Assert.notNull(callbackType, "callback type not found");
        return payCallbackServiceMap.get(callbackType);
    }

    /**
     * 判断支付回调的类型
     * @author nassir.wen
     * @date 2013-1-7下午4:49:06
     * @param request
     * @return
     */
    private String judgeCallbackType(HttpServletRequest request) {
        Set<Entry<String, String>> entry = payMerIdParamMap.entrySet();
        for (Entry<String, String> e : entry) {
            String value = request.getParameter(e.getKey());
            if (StringUtils.isNotEmpty(value)) {
                return e.getValue();
            }
        }
        return null;
    }

    public Map<String, PayCallbackService> getPayCallbackServiceMap() {
        return payCallbackServiceMap;
    }

    public void setPayCallbackServiceMap(Map<String, PayCallbackService> payCallbackServiceMap) {
        this.payCallbackServiceMap = payCallbackServiceMap;
    }

    public Map<String, String> getPayMerIdParamMap() {
        return payMerIdParamMap;
    }

    public void setPayMerIdParamMap(Map<String, String> payMerIdParamMap) {
        this.payMerIdParamMap = payMerIdParamMap;
    }

}
